package com.shawn.baseappbygroovydsl.utils

import android.content.res.Resources
import android.util.TypedValue
import com.shawn.baseappbygroovydsl.base.AppBase

object ExtensionCommon {
    val Float.dp
        get() = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            this,
            Resources.getSystem().displayMetrics
        )


    val Float.sp
        get() = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            this,
            Resources.getSystem().displayMetrics
        )

    fun Float.dp2px(): Int {
        val scale = AppBase.instance.resources?.displayMetrics?.density ?: 1f
        return (this * scale + 0.5f).toInt()
    }

    fun Float.px2dp(): Int {
        val scale = AppBase.instance.resources?.displayMetrics?.density ?: 1f
        return (this / scale + 0.5f).toInt()
    }
}